<?php
include_once("../includes/global.php");
$script_tmp = explode('/', $_SERVER['SCRIPT_NAME']);
$sctiptName = array_pop($script_tmp);
include_once("auth.php");
include_once("../includes/page_utf_class.php");
//==========================================
if(!empty($_POST["action"])&&$_POST["action"]==lang_show('delete'))
{
	if(isset($_POST["de"]) && is_array($_POST["de"]))
	{
		$id=implode(",",$_POST["de"]);
		if($id)
			$db->query("update ".ADVSCON." set statu='-2' where id in ($id) and statu=-1");
	}
}
elseif('audit'==$_GET["submit"]||!empty($_POST['audit']) )
{			 
	include_once("includes/plugin_payment_class.php");
	$payment =new payment();
	if(isset($_GET['submit'])&&isset($_GET['id'])&&isset($_GET['audit']))
	{
		$sub_sql = '';
		if('-1'==$_GET['audit'])
			$payment->refund_adv( $_GET['id'] );
		else
			$sub_sql.=' and statu>-1';
		$sql="update ".ADVSCON." set statu='$_GET[audit]' where ID='$_GET[id]' $sub_sql";
		$db->query($sql);
		msg(urldecode($_GET['fw']));
	}
	if(isset($_POST["de"]) && is_array($_POST["de"]))
	{
		$id=implode(",",$_POST["de"]);
		$audit = $_POST['audit'];
		$sub_sql = '';
		 if($audit<0)
		{
			$db->query("select id from ".ADVSCON." where  id in ($id) and amount>0");
			while($ad=$db->fetchRow())
			{print_r($ad);die();
				$payment->refund_adv( $ad[0] );		//广告退款
			}
		 }
		 else
			$sub_sql .= ' and amount>0';
		$sql="update ".ADVSCON." set statu='$audit' where id in ($id) $sub_sql";
		$db->query($sql);
	}
}

$db->query("select * from ".ADVS." order by ID asc");
$adv_groups = array();
while($pl=$db->fetchRow())
	$adv_groups[$pl['ID']] = $pl;

$audit_status[] = '-1,不通过';
$audit_status[] = '0,待审核';
$audit_status[] = '1,审核通过';
$audit_list = urlencode(implode('|',$audit_status));
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<TITLE><?php echo lang_show('admin_system');?></TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../script/prototype.js"></script>
<script type="text/javascript" src="main.js"></script>
<script src="../script/my_lightbox.js" language="javascript"></script>
<script>
var weburl = "<?php echo $config['weburl'];?>";
function setAudit(v)
{
	if(arguments[1])
		window.location.href = weburl+"/admin/adv_audit.php?submit=audit&id="+arguments[1]+'&audit='+v+'&fw='+encodeURIComponent(window.location.href);
	else{
		$('audit').value = v;
		document.frm.submit();
	}
}
function multi_admin()
{
	if(chk_select('de[]')){
		var statu = "<?php echo $audit_list;?>";
		alertWin("<?php echo lang_show('multi_audit');?>",'',360,240,"admin_audit.php?&status="+statu);
	}
}
function chk_select(name)
{
	 var box_l = document.getElementsByName(name).length;
	 for(var j = 0 ; j < box_l ; j++)
	 {
	  	if(document.getElementsByName(name)[j].checked==true)
			return true;
	 }
	 alert("<?php echo lang_show('select_chk');?>");
	 return false;
}
</script>
</HEAD>
<body>

  <div class="bigbox">
	<div class="bigboxhead"><?php echo lang_show('adv_audit');?></div>
	<div class="bigboxbody">
		<form method="get"  action="">
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
       <td><?php echo lang_show('statu');?></td>
       <td>
       <?php
          $status=array(
              '4'=>lang_show('all'),
              '-1'=>lang_show('notpass'),
			  '0'=>lang_show('wpass'),
              '1'=>lang_show('auditpass'),
			  '-2'=>lang_show('out_time'),
          );
      ?> 
        <select class="select" name="statu">
        <?php
        foreach($status as $key=>$v)
        {
        ?>
          <option value="<?php echo $key;?>" <?php if(isset($_GET['statu'])&&$_GET['statu']==$key)echo "selected";?>>
            <?php echo $v;?>
          </option>
        <?php
         } 
        ?>
        </select>
       </td>
     </tr>
    <tr>
      <td>选项</td>
      <td><select class="select" name="gid">
		<option value=''><?php echo lang_show('all'); ?></option>
	  <?php
		foreach($adv_groups as $key=>$v)
		{
		?>
		<option value="<?php echo $key;?>" <?php if(!empty($_GET['gid'])&&$_GET['gid']==$key)echo "selected";?>> <?php echo $v['name'];?> </option>
		<?php
		 }
		 ?>
        </select></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>
	    <input class="btn" type="submit" name="Submit" value="<?php echo lang_show('search');?>">
       </td>
    </tr>
</table>
</form>

<form action="" name='frm' method="POST" onsubmit="return chk_select('de[]')">
	  <table width="100%" border="0" cellpadding="2" cellspacing="0" >
		<tr class="theader"> 
		  <td width="25"><input onClick="do_select()" type="checkbox" name="checkbox" value="checkbox"></td>
		  <td width="200"><?php echo lang_show('adv_name');?></td>
		  <td width="100"><?php echo lang_show('guser');?></td>
		  <td width="98" align="left"  ><?php echo lang_show('time');?></td>
		  <td width="98" align="left"  ><?php echo lang_show('adv_showtime');?></td>
		  <td width="98" align="left"  ><?php echo lang_show('adv_amount');?></td>
		  <td width="68" align="left" ><?php echo lang_show('statu');?></td>
		</tr>
		<?php
		$scl="";
		if(!empty($_GET["gid"]))
		{
				$scl.=" AND a.group_id='$_GET[gid]' ";
		}
	
		if(isset($_GET["statu"])&&$_GET["statu"]<4)
			$scl.=$_GET["statu"]==-2?" AND  a.etime>0 and a.etime<'".time()."'":" AND a.statu=".intval($_GET['statu']);
		else
			$scl.=" AND a.statu>-2 ";
			
		$sql="select a.*,u.user	 from ".ADVSCON."  a left join ".USER."  u on a.userid=u.userid  where a.userid>0 $scl order by a.ID desc ";
		//=============================
		$page = new Page;
		$page->listRows=20;
		if (!$page->__get('totalRows')){
			$db->query("select count(*) as total	from ".ADVSCON." a  where a.userid>0 $scl");
			$page->totalRows = $db->fetchField('num');
		}
		$sql .= "  limit ".$page->firstRow.",20";
		$pages = $page->prompt();
		//=====================
		$db->query($sql);
		$re=$db->getRows();
		foreach($re as $v)
		{
		?> 
      <tr  onMouseOver="mouseOver(this)" onMouseOut="mouseOut(this,'odd')"> 
         <td width="25"><input name="de[]" type="checkbox" id="de"  <?php if($v['statu']==-1||($v['etime']>0&&$v['etime']<time())) echo "disabled='disabled'";?> value="<?php echo $v['ID']; ?>"></td>
         <td width="200"><?php echo $v['name'];?></td>
		 <td width="100"><?php echo $v['user'];?></td>
		 <td width="100"><?php echo date('Y-m-d H:i:s',$v['ctime']);?></td>
         <td  align="left">&nbsp;<?php echo $v['show_time'].lang_show($v['unit']); ?></td>
		 <td  align="left">&nbsp;<?php if($v['statu']==-1&&$v['amount']==0){ echo "<font color='green'>".lang_show('refund_money').'</font>';}else{ echo $config['money'].$v['amount']."  ($config[money]".$adv_groups[$v['group_id']]['price'].'/'.lang_show($v['unit']).')'; }?></td>
		 <td  align="left">
			<?php if($v['statu']==-1){  echo "<font color='red'>".$status[$v['statu']].'</font>';}elseif($v['etime']>0&&$v['etime']<time()){
				echo "<font color='#666'>".lang_show('out_time').'</font>';
			}else{ ?><a href="javascript:alertWin('<?php echo lang_show('statu');?>','',360,240,'admin_audit.php?&status=<?php echo $audit_list;?>&id=<?php echo $v['ID']; ?>&statu=<?php echo $v['statu'];?>');">
			<?php  
			echo $status[$v['statu']].'</a>'; }
			?> </td>
      </tr>
    <?php 
    }
	?> 
  </table>
  <table width="100%" height="20" border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td width="45%">
	 <!-- <input class="btn" type="submit" name="action" value="<?php echo lang_show('delete');?>" onClick="return chk_select('de[]')&&confirm('<?php echo lang_show('are_you_sure');?>');">-->
	  <input class="btn"  type="submit" name="action" onclick='multi_admin();return false;' type='submit' value='<?php echo lang_show('multi_audit');?>' />
	  <input type='hidden' value='' name='audit' id='audit' />
	  </td>
      <td width="65%" align="right"><div class="page"><?php echo $pages?></div></td>
    </tr>
  </table>
  </form>
  </div>
</div>
</body>
</html>